/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 74646
 * Date: 2022-02-27
 * Time: 22:41
 */
public class MyCircularQueue {
    public int[] elem;
    public int front; //队头下标
    public int rear; //队尾下标

    public MyCircularQueue(int k) {
        this.elem = new int[k+1];
    }

    /**
     * 入队
     * @param value
     * @return
     */
    public boolean enQueue(int value) {
        if(isFull()){
            return false;
        }
        this.elem[rear] = value;
        rear = (rear+1)%elem.length;
        return true;
    }

    /**
     * 出队
     * @return
     */
    public boolean deQueue() {
        if(isEmpty()){
            return false;
        }
        front = (front+1)%elem.length;
        return true;
    }

    /**
     * 得到队头元素
     * @return
     */
    public int Front() {
        if(isEmpty()){
            //OJ上需要返回-1；自己写可以抛出一个异常
            return -1;
        }
        return this.elem[front];
    }

    /**
     * 得到队尾元素
     * @return
     */
    public int Rear() {
        if(isEmpty()){
            return -1;
        }
        if(rear==0){
            return elem[elem.length-1];
        }else{
            return elem[rear-1];
        }
       // return (rear+elem.length-1)%elem.length;
    }

    /**
     * 判断队列是否为空
     * @return
     */
    public boolean isEmpty() {
       return front == rear;
    }

    /**
     * 判断队列是否满了
     * @return
     */
    public boolean isFull() {
        //如果rear的下一个下标是front，表示队列已满
        if((rear+1)%elem.length == front){
            return true;
        }
        return false;
    }
}
